package com.zdhy.zoc.collect.snmp;

import com.sun.java.swing.plaf.windows.resources.windows;
import com.zdhy.zoc.collect.snmp.model.ModelSnmp;
import com.zdhy.zoc.collect.snmp.model.Windows;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 梁浩峰 on 2016/8/24 15:09.
 */
public class SNMPTest {
    public static void walk(String hosts, String port, String readName,String oid) {
        try {
            Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); // 构造一个UDP
            snmp.listen(); // 开始监听snmp消息

            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(readName));// snmpv2的团体名
            target.setVersion(SnmpConstants.version2c); // snmp版本
            target.setAddress(new UdpAddress(hosts+"/"+port));
            target.setTimeout(60000); // 时延
            target.setRetries(1); // 重传

            TableUtils utils = new TableUtils(snmp, new DefaultPDUFactory(
                    PDU.GETNEXT));// GETNEXT or GETBULK
            utils.setMaxNumRowsPerPDU(5); // only for GETBULK, set max-repetitions, default is 10
            OID[] columnOids = new OID[] {new OID(oid)};
            // If not null, all returned rows have an index in a range (lowerBoundIndex, upperBoundIndex]
            //lowerBoundIndex,upperBoundIndex都为null时返回所有的叶子节点。 必须具体到某个OID,,否则返回的结果不会在(lowerBoundIndex, upperBoundIndex)之间
            List<TableEvent> l = utils.getTable(target, columnOids,null
                    ,null);
            for (TableEvent e : l) {
                VariableBinding[] bindings = e.getColumns();
                String s = bindings[0].toValueString();
                if(s!=null&&!s.equals("")){
                    System.out.println( bindings[0].getOid()+"==>"+s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        String oid="1.3.6.1.2.1.25.2.3.1.5";


        if(args.length<3){
            System.out.println("至少三个参数");
            System.exit(-1);
        }else{
//            HashMap<OID, String> map = SnmpUtil.snmpWalk(args[0], args[1], new OctetString(args[2]), oid);
            Windows windows1 = new Windows(args[0], args[1], args[2], SnmpUtil.SNMP_V2);
            ModelSnmp modelSnmp = windows1.walkData(windows1);
//            for (Map.Entry<OID,String> en:map.entrySet()
//                 ) {
//                System.out.println(en.getKey()+"==>"+en.getValue());
//            }

            System.out.println(modelSnmp.getWorkStatus()+"--"+modelSnmp.getWorkCpu()+"--"+modelSnmp.getWorkmen());
        }
    }
}
